package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TreeSelectStocks;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.StocksMapper;
import com.ruoyi.system.domain.Stocks;
import com.ruoyi.system.service.IStocksService;

/**
 * 仓库管理Service业务层处理
 * 
 * @author he
 * @date 2025-09-16
 */
@Service
public class StocksServiceImpl implements IStocksService 
{
    @Autowired
    private StocksMapper stocksMapper;

    /**
     * 查询仓库管理
     * 
     * @param id 仓库管理主键
     * @return 仓库管理
     */
    @Override
    public Stocks selectStocksById(Long id)
    {
        return stocksMapper.selectStocksById(id);
    }

    /**
     * 查询仓库管理列表
     * 
     * @param stocks 仓库管理
     * @return 仓库管理
     */
    @Override
    public List<Stocks> selectStocksList(Stocks stocks)
    {
        return stocksMapper.selectStocksList(stocks);
    }

    /**
     * 新增仓库管理
     * 
     * @param stocks 仓库管理
     * @return 结果
     */
    @Override
    public int insertStocks(Stocks stocks)
    {
        stocks.setCreateTime(DateUtils.getNowDate());
        stocks.setCreateUserId(SecurityUtils.getUserId());
        stocks.setCreateUserName(SecurityUtils.getUsername());

        //处理父级
        if (ObjectUtils.isEmpty(stocks.getPid())){
            stocks.setPid(Long.valueOf(0));
            stocks.setPidPath("0");
        } else {
            String pidPath = stocks.getPidPath();
            //拼接路径
            String newPidPath = pidPath + ',' + stocks.getPid();
            stocks.setPidPath(newPidPath);
        }


        return stocksMapper.insertStocks(stocks);
    }

    /**
     * 修改仓库管理
     * 
     * @param stocks 仓库管理
     * @return 结果
     */
    @Override
    public int updateStocks(Stocks stocks)
    {
        stocks.setUpdateTime(DateUtils.getNowDate());
    stocks.setUpdateUserId(SecurityUtils.getUserId());
    stocks.setUpdateUserName(SecurityUtils.getUsername());
        return stocksMapper.updateStocks(stocks);
    }

    /**
     * 批量删除仓库管理
     * 
     * @param ids 需要删除的仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteStocksByIds(Long[] ids)
    {
        return stocksMapper.deleteStocksByIds(ids);
    }

    /**
     * 删除仓库管理信息
     * 
     * @param id 仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteStocksById(Long id)
    {
        return stocksMapper.deleteStocksById(id);
    }

    @Override
    public List<Stocks> getStocksInfoList(String paramName) {
        return stocksMapper.getStocksInfoList(paramName);
    }

    @Override
    public List<TreeSelectStocks> getStocksTree(String paramName) {
        Stocks stocks = new Stocks();
        stocks.setName(paramName);
        List<Stocks> stocks1 = stocksMapper.selectStocksList(stocks);
        List<TreeSelectStocks> treeSelects = buildDeptTree(stocks1);
        return treeSelects;
    }


    public List<TreeSelectStocks> buildDeptTree(List<Stocks> depts)
    {
        List<Stocks> returnList = new ArrayList<Stocks>();
        List<Long> tempList = depts.stream().map(Stocks::getId).collect(Collectors.toList());
        for (Stocks dept : depts)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getPid()))
            {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }

        return returnList.stream().map(TreeSelectStocks::new).collect(Collectors.toList());

    }

    private void recursionFn(List<Stocks> list, Stocks t)
    {
        // 得到子节点列表
        List<Stocks> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Stocks tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<Stocks> getChildList(List<Stocks> list, Stocks t)
    {
        List<Stocks> tlist = new ArrayList<Stocks>();
        Iterator<Stocks> it = list.iterator();
        while (it.hasNext())
        {
            Stocks n = (Stocks) it.next();
            if (StringUtils.isNotNull(n.getPid()) && n.getPid().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Stocks> list, Stocks t)
    {
        return getChildList(list, t).size() > 0;
    }


}
